package fpc.controls.buttons;
import fpc.styles.skins.IFixedButtonSkin;
import fpc.styles.skins.NullFixedButtonSkin;
import fpc.styles.skins.SkinType;
import fpc.styles.Style;
import fpc.controls.text.Label;
import fpc.utils.ClassFactory;
import fpc.utils.IFactory;

class RadioButton extends AButton
{	
	var _skinType:SkinType<IFixedButtonSkin>;
	
	public function new(label:String = "", selected:Bool = false)
	{
		_label = new Label(label);
		
		super();
		
		_label.parent = this;
		_label.addTo(_sprite);
		
		toggle = true;
		if (selected)
		{
			this.selected = selected;
		}
	}
	
	override function render()
	{
		_buttonSkin.update();
	}
	
	var _label:Label;
	
	var _fixedSkin:IFixedButtonSkin;
	
	var _skinFactory:IFactory<IFixedButtonSkin>;
	
	override function updateStyle()
	{
		var skinFactory = Style.findSkin(this, getSkinType());
		if (skinFactory != _skinFactory)
		{
			var skin = skinFactory.create();
			_fixedSkin = skin;
			
			_label.x = skin.width;
			applyButtonSkin(skin);
		}
	}
	
	function getSkinType()
	{
		return SKIN_TYPE_RADIO_BUTTON;
	}
	
	override function tryResize(width:Float, heght:Float)
	{
		_width = _label.x + _label.width;
		_height = _fixedSkin.height;
	}
	
	public static var SKIN_TYPE_RADIO_BUTTON = new SkinType<IFixedButtonSkin>(
		new ClassFactory<IFixedButtonSkin>(NullFixedButtonSkin)
	);
}
